package fileDemo.excel;

import entity.Person;
import fileDemo.ILoadDataCallback;
import org.junit.Test;
import utils.PrintUtil;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;

/**
 * @author Ajie
 * @date 4/29/21
 * @function
 * 测试 Excel 读写
 *
 * 参考 datatool
 * com.opnext.datatool.model.AddPeopleModel#addPeopleResult
 *
 */
public class ExcelHome {
    //    private File template = new File("src/fileDemo/excel/template.xlsx");
    private File readerPutPath = new File("src/fileDemo/excel/testWriter.xlsx");
    private File writerOutPutPath = new File("src/fileDemo/excel/testWriter.xlsx");

    @Test
    public void testWriter() {

        ExcelWriter excelWriter = new ExcelWriter() {
            public void generate() throws Exception {

                // 电子表格开始
                beginSheet();

                //insert Header
                insertRow(0);
                int mHeader = 5;
                for (int i = 0; i < mHeader; i++) {
                    createCell(i, "Header" + i);
                }
                endRow();

                //insert data
                int dataRows = 10;
                for (int i = 0; i < dataRows; i++) {
                    insertRow(i + 1);
                    for (int j = 0; j < mHeader; j++) {
                        createCell(j, "data row:" + i + " col:" + j);
                    }
                    endRow();
                }
                // 电子表格结束
                endSheet();
            }
        };
        try {
            excelWriter.process(writerOutPutPath.getAbsolutePath(), writerOutPutPath.getAbsoluteFile().getParent());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //TODO bug .ExcelWriter 写完后 ExcelReader 直接读，【读不出来]！！！
    // wps 打开编辑下，在还原。保存。不更改内容。就可以读出来。
    // ExcelWriter2 写完后，ExcelReader 可以直接读出来！！！
    @Test
    public void testWriter2(){
        ArrayList<Person> personList = new ArrayList<>();
        for (int i = 0; i <5; i++) {
            personList.add(new Person("name"+i,10+i));
        }
        ExcelWriter2.writePerssonsToExcelByBatch(personList, writerOutPutPath.getAbsolutePath(), new ILoadDataCallback() {
            @Override
            public void onResult(int resultCode, Object data) {
                System.err.println("result,resultCode:"+resultCode+"  data:"+data);
            }

            @Override
            public void onProgress(int value) {
                System.out.println("onProgress:"+value);
            }
        });
    }

    //TODO bug .ExcelWriter 写完后 ExcelReader 直接读，【读不出来]！！！
    // wps 打开编辑下，在还原。保存。不更改内容。就可以读出来。
    // ExcelWriter2 写完后，ExcelReader 可以直接读出来！！！
    @Test
    public void testReader() {
        long timeStamp = System.currentTimeMillis();

        ExcelReader reader = new ExcelReader() {
            @Override
            public void getRowData(int rowSize, String sheetIndex, int rowIndex, String[] rowList) {
                if (rowIndex == 0) {
                    System.out.println("header:"+"  rowList:"+rowList.length);
                    PrintUtil.print(rowList);
                } else {
                    System.out.println("rowIndex:"+rowIndex +"  rowList:"+rowList.length);
                    PrintUtil.print(rowList);
                    //处理业务
//                    addPerson(rowList, timRule, photoPath);
//                    PersonEntity entity = new PersonEntity(rowList);
//                    PersonInfo person = new PersonInfo();
//                    String photo = entity.getPhotoPath();
                    //记录错误的行数
                    int progress = (int) (((float) rowIndex) / rowSize * 100);
                    System.out.println("read progress: " + progress);
                }
            }

            @Override
            public void onResult(int rowSize, String SheetIndex) {
                System.out.println("getGetRowDataCount():"+getGetRowDataCount() );
/*                //处理错误
                AddPeopleResult result = new AddPeopleResult();
                result.setSum(getGetRowDataCount() - 1); // ignore header
                result.setFaliedCount(mErrorList.size());
                result.setSuccessCount(result.getSum() - result.getFaliedCount());
                if (mErrorList.size() > 0) {
                    //生成错误的 结果反馈
                    File file = new File(excelPath);
                    long timeStamp = System.currentTimeMillis();
                    SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
                    String data = format.format(new Date(timeStamp));
                    String path = file.getParent() + "/fail_" + data;
                    result.setPath("/fail_" + data);
                    writePeopleToExcel(context, path, photoPath);
                    mErrorList.clear();
                }
                //结果回调
                cb.onProgress(100);
                cb.onResult(Utils.RESULT_SUCCESS, result);*/
                long time = System.currentTimeMillis() - timeStamp;
                System.err.println("===================end add people " + time + "ms ===================");
            }
        };
        try {
            reader.processOneSheet(readerPutPath.getAbsolutePath());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
